#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
#Leo Zhang@2008
#

from google.appengine.ext import db
from google.appengine.ext.db import djangoforms

class RSSSite(db.Model):
  domain = db.StringProperty(required=True)
  desc = db.StringProperty(required=True)
  #easy read for the choice
  def __str__(self):
      return self.domain
  
class SiteChannel(db.Model):
  site = db.ReferenceProperty(RSSSite, collection_name='childRSSes')
  rank = db.IntegerProperty()
  label = db.StringProperty(required=True)
  rssURL = db.LinkProperty(required=True)
  isListed = db.BooleanProperty(default=False)
  updateTime = db.DateTimeProperty(auto_now_add=True)
  #easy read for the choice
  def __str__(self):
      return self.label

class Pagekey(db.Model):
  """
  define how to get and what type content to get.
  every pagekey refered to one site, we can get pagekey by site.site_Pagekeys
  """
  site = db.ReferenceProperty(RSSSite, collection_name='assPagekeys')
  rank = db.IntegerProperty()
  pattern = db.StringProperty(required=True)
  charset = db.StringProperty(required=True, choices=set(["gbk","utf8"]))
  method = db.StringProperty(required=True)
  textTags = db.StringProperty(required=True, multiline=True)
  updateTime = db.DateTimeProperty(auto_now_add=True)
  
  def isMatched(self,url):
    if re.match(self.pattern, url, re.I):
        return True
    else:
        return False

  def getTags(self):
    tags =[]
    for line in self.textTags.splitlines():
        tkv = line.split(':')
        kv = tkv[1].split(',')
        tags.append([tkv[0],(kv[0],kv[1])])
    return tags